package dao;

import entities.Department;
import wrappers.*;
import exceptions.DatabaseException;
import java.sql.*;
import java.util.List;

/**
 *
 * @author KAV
 */
public class DepartmentDAO extends AbstractDAO {

    @Override
    protected PreparedStatement stringForInsert(Connection c, Object obj) throws DatabaseException {
        try {
            Department e = (Department) obj;
            PreparedStatement st = c.prepareStatement("INSERT INTO departments(title, country) VALUES (?, ?)");
            st.setString(1, e.getTitle());
            st.setInt(2, e.getCountry());
            return st;
        } catch (SQLException ex) {
            throw new DatabaseException(ex);
        }
    }

    @Override
    protected PreparedStatement stringForDeleteById(Connection c, int id) throws DatabaseException {
        try {
            PreparedStatement st = c.prepareStatement("DELETE FROM departments WHERE id=?");
            st.setInt(1, id);
            return st;
        } catch (SQLException ex) {
            throw new DatabaseException(ex);
        }
    }

    @Override
    protected PreparedStatement stringForUpdateById(Connection c, Object obj) throws DatabaseException {
        try {
            Department e = (Department) obj;
            PreparedStatement st = c.prepareStatement("UPDATE departments SET title=?, country=? WHERE id=?");
            st.setString(1, e.getTitle());
            st.setInt(2, e.getCountry());
            st.setInt(3, e.getDepartment());
            return st;
        } catch (SQLException ex) {
            throw new DatabaseException(ex);
        }
    }

    @Override
    protected List<Object> processForSelectAll(Connection c) throws DatabaseException {
        List<Object> list = new java.util.LinkedList<Object>();
        try {
            PreparedStatement st = c.prepareStatement("SELECT d.id, d.title, d.country , c.title as countryName FROM departments d, countries c WHERE d.country=c.id ");
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                Department depart = new Department();
                depart.setDepartment(rs.getInt("id"));
                depart.setTitle(rs.getString("title"));
                depart.setCountry(rs.getInt("country"));
                DepartmentWrap ew = new DepartmentWrap(depart);
                ew.setViewCountry(new ViewElement("country", rs.getString("countryName")));
                list.add((Object) ew);
            }
        } catch (SQLException ex) {
            throw new DatabaseException(ex);
        }
        return list;
    }

    @Override
    protected Object processForSelectId(Connection c, int id) throws DatabaseException {
        Object obj = new Object();
        try {
            PreparedStatement st = c.prepareStatement("SELECT * FROM departments WHERE id=?");
            st.setInt(1, id);
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                Department e = new Department();
                e.setCountry(rs.getInt("country"));
                e.setTitle(rs.getString("title"));
                e.setDepartment(rs.getInt("id"));
                obj = (Object) e;
                break;
            }
        } catch (SQLException ex) {
            throw new DatabaseException(ex);
        }
        return obj;
    }

    @Override
    public List<Object> findByNameOrSurname(String query) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
